﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using QxFramework.Core;
using App.Common;
using DG.Tweening;
public class RayBullet : BulletBase
{
    public override void OnEnable()
    {
        base.OnEnable();
        GetComponent<LineRenderer>().enabled = false;
        StartCoroutine(MainLogic());
    }
    IEnumerator MainLogic()
    {
        yield return 0;
        GetComponent<LineRenderer>().SetPosition(0, bulletBase.positionData.Position);
        GetComponent<LineRenderer>().enabled = true;
        GetComponent<LineRenderer>().startColor = Color.white;
        GetComponent<LineRenderer>().endColor = GetAttrColor();
        float Dis1=0;
        float Dis2=0;
        if (IsHitted(bulletBase.positionData.StartPosition, bulletBase.Direction, out Vector3 HittedPoint2, out GameObject HittedThing2, "Obstacle"))
        {
            Dis1 = (bulletBase.positionData.Position - HittedPoint2).magnitude;
        }
        if (IsHitted(bulletBase.positionData.StartPosition, bulletBase.Direction, out Vector3 HittedPoint, out GameObject HittedThing, "Monos"))
        {
            Dis2 = (bulletBase.positionData.Position - HittedPoint).magnitude; ;
        }
        if (Dis1 > 0 && (Dis1 < Dis2 || Dis2 == 0))
        {
            HitOthersEffect(HittedPoint2);
            GetComponent<LineRenderer>().SetPosition(1, HittedPoint2);
        }
        else if (Dis2 > 0 && (Dis2 < Dis1 || Dis1 == 0))
        {
            if (HittedThing.GetComponent<MonoBase>() != null && (bulletBase.positionData.StartPosition - HittedPoint).magnitude < bulletBase.Range)
            {
                HitAddBuff(HittedThing);
                HitOthersEffect(HittedPoint);
                GetComponent<LineRenderer>().SetPosition(1, HittedPoint);
            }
        }
        else
        {
            GetComponent<LineRenderer>().SetPosition(1, bulletBase.positionData.StartPosition + bulletBase.Direction * bulletBase.Range);
            HitOthersEffect(bulletBase.positionData.StartPosition + bulletBase.Direction * bulletBase.Range);
        }
        yield return new WaitForSeconds(0.05f);
        OnDestroyed();
    }
    //private Color GetAttrColor()
    //{
    //    switch (bulletBase.attribute)
    //    {
    //        case MonoAttribute.None:
    //            return new Color(1, 1, 1, 1);
    //        case MonoAttribute.Fire:
    //            return Data.Instance.PrintColor[0];
    //        case MonoAttribute.Ice:
    //            return Data.Instance.PrintColor[1];
    //        case MonoAttribute.Shine:
    //            return Data.Instance.PrintColor[2];
    //        case MonoAttribute.Dark:
    //            return Data.Instance.PrintColor[3];
    //        default:
    //            return new Color(1, 1, 1, 1);
    //    }
    //}
    public override void OnDestroyed()
    {
        base.OnDestroyed();
        Destoyed();
    }
    private void Destoyed()
    {
        GameMgr.Get<IBulletManager>().RemoveMono(gameObject.name);
    }

    /// <summary>
    /// 判断击中
    /// </summary>
    public bool IsHitted(Vector2 Start, Vector2 Direct,out Vector3 HittedPoint,out GameObject HittedThing,string Layer)
    {
        bool IsHit = false;
        HittedPoint = Vector3.zero;
        HittedThing = null;
        LayerMask MonoLayer = 1 << LayerMask.NameToLayer(Layer);
        RaycastHit2D hit = Physics2D.Raycast(Start + Direct.normalized, Direct, 1000, MonoLayer);
        if (!hit)
        {
            return false;
        }
        if (hit.collider.gameObject != gameObject)
        {
            IsHit = true;
            HittedPoint = hit.point;
            HittedThing = hit.collider.gameObject;
        }
        return IsHit;
    }

}
